package com.smiier.skin;

import java.util.ArrayList;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.util.OUtil;
import cn.skinapp.R;

import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.pifuke.adapter.TreatmentCommentsAdapter;
import com.smiier.skin.extra.TreatmentProjectItemExtra;
import com.smiier.skin.net.DoctorTreatmentProjectListTask.DoctorTreatmentProjectItem;
import com.smiier.skin.net.TreatmentajaxProjectCommentsListTask;
import com.smiier.skin.net.TreatmentajaxProjectCommentsListTask.TreatmentajaxProjectCommentsListRequest;
import com.smiier.skin.net.TreatmentajaxProjectCommentsListTask.TreatmentajaxProjectCommentsListResponse;
import com.smiier.skin.net.entity.Comment;
import com.smiier.skin.ui.BasicActivity;
import com.umeng.analytics.MobclickAgent;

public class CommentListActivity extends BasicActivity implements
		OnRefreshListener<ListView> {
	PullToRefreshListView mListView;
	TreatmentCommentsAdapter mAdapter;
	ArrayList<Comment> mArray = new ArrayList<Comment>();
	TreatmentProjectItemExtra extra = new TreatmentProjectItemExtra();
	DoctorTreatmentProjectItem item;
	int start = 0;
	int end = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comments_list);
		init();
		extra.getFrom(getIntent());
		item = extra.getUser();
		setNavTitle("更多评价");
		TextView head = new TextView(getContext());
		head.setBackgroundResource(R.drawable.bg_bottom_border_normal);
		head.setPadding(0, (int) OUtil.dp2px(getContext(), 12), 0,
				(int) OUtil.dp2px(getContext(), 12));
		head.setText("用户评价");
		head.setTextSize(14);
		head.setTextColor(Color.BLACK);

		mListView = findViewById(R.id.list_comments,
				PullToRefreshListView.class);
		mListView.setOnRefreshListener(this);
		mListView.setPullRefreshEnabled(true);
		ListView list = mListView.getListView();
		list.addHeaderView(head);
		/*
		 * list.setDivider(getResources().getDrawable(R.drawable.line));
		 * list.setDividerHeight(1);
		 */
		mListView.setScrollLoadEnabled(true);
		mListView.doPullRefreshing(true);
		mAdapter = new TreatmentCommentsAdapter();
		mListView.setAdapter(mAdapter);

	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		start = 0;
		end = start + 5;
		loadData();
	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {
		start = end;
		end = start + 5;
		loadData();
	}

	private void loadData() {
		TreatmentajaxProjectCommentsListTask task = new TreatmentajaxProjectCommentsListTask();
		TreatmentajaxProjectCommentsListRequest req = new TreatmentajaxProjectCommentsListRequest();
		req.treatmentprojectid = item.id;
		req.start = start;
		req.end = end;

		task.setRequest(req);
		task.addListener(new NetTaskListener<TreatmentajaxProjectCommentsListTask.TreatmentajaxProjectCommentsListRequest, TreatmentajaxProjectCommentsListTask.TreatmentajaxProjectCommentsListResponse>() {

			@Override
			public void onException(
					INetTask<TreatmentajaxProjectCommentsListRequest, TreatmentajaxProjectCommentsListResponse> task,
					Exception e) {
				mListView.setContextEmptyType(true, 2);
			}

			@Override
			public void onComplete(
					INetTask<TreatmentajaxProjectCommentsListRequest, TreatmentajaxProjectCommentsListResponse> task,
					TreatmentajaxProjectCommentsListResponse response) {
				mListView.doComplete();
				if (response != null && response.size() > 0) {
					int size = task.getRequest().end;
					if (size > mArray.size()) {
						mArray.addAll(response);
						mAdapter.setDataProvider(mArray);
						mAdapter.notifyDataSetChanged();
					}
				}
				if (mArray.size() < 1) {
					mListView.setContextEmptyType(true, 2);
				} else {
					mListView.setContextEmptyType(false, 2);
				}
			}
		});
		add(task);
	}
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
	
	
}
